<template>
	<view><view class="content" :class="{'active':active}">
		<view class="header_top">
			鲨鱼记账
		</view>
		<view class="tabbar-box-wrap">
			<view class="tabbarClick">
				<view class="zhichu" :class="selectIndex===0?'active':''" @click="tabbarClick(0)">
					支出
				</view>
				<view class="shouru" :class="selectIndex===1?'active':''" @click="tabbarClick(1)">
					收入
				</view>
				<view class="cancel" @click="qx">
					取消
				</view>
			</view>
			<view class="tabbar-box">
				<view class="tabbar-box-item" v-for="(item,index) in iconList" :key="index"
					@click="tabbarboxitemfun(item,index)">
					<view class="img_warp" :style="{background:getRandomColor(index,iconList.length)}"
						:class="selectItem===index?'item_active':''">
						<!-- <image class="box-image" :src="item.icon" mode="aspectFit"></image> -->
						{{item.name.charAt(0)}}
					</view>
					<view class="explain" :class="selectItem===index?'item_active1':''">{{item.name}}</view>

				</view>
			</view>
		</view>
		<view class="keybordWarp">
			<jia-PayKeyboard :overlayClose="true" ref="keyWord" v-model="num" @input="inp1" @confirm="inp" @hide="hide">
				<view class="money_warp">
					{{num}}
				</view>
				<view class="beizhu_warp">
					<text>备注:</text>
					<input type="text" placeholder="点击填写备注" v-model="beizhuText" />
				</view>
			</jia-PayKeyboard>
		</view>
	</view></view>
</template>

<script>
	export default {
		data() {
			return {
				confirmStyle: {
					// backgroundColor:'yellow',
					// color:'#000'
				},
				currentlySelected: {},
				num: "",
				selectItem: 0,
				selectIndex: 0,
				beizhuText: "",
				active: false,
				iconList: [{
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, {
					name: '餐饮',
					icon: '../../../static/img/release.png'
				}, ]
			};
		},
		// onLoad() {},
		mounted() {
			// setTimeout(() => {
			this.active = true;
			// }, 500);
		},
		onUnload() {
			this.active = false;
		},
		methods: {
			qx(){
				this.$emit('Cancel')
			},
			tabbarClick(val) {
				this.selectIndex = val

				if (val === 0) {
					this.iconList = [{
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, {
						name: '餐饮',
						icon: '../../../static/img/release.png'
					}, ]

				} else {

					this.iconList = [{
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}, {
						name: '吃饭',
						icon: '../../../static/img/release.png'
					}]
				}
			},

			tabbarboxitemfun(item, val) {
				console.log(item, val)
				this.currentlySelected = item
				this.selectItem = val
				this.$refs.keyWord.open();

			},
			getRandomColor(index) {
				const base = index * 13; // 使用索引作为基础，乘以一个常数来改变起始点
				const r = Math.floor(Math.random() * 256) + base; // 在基础值上加上随机偏移量
				const g = Math.floor(Math.random() * 256) + base; // 同上
				const b = Math.floor(Math.random() * 256) + base; // 同上
				return `rgb(${r % 256}, ${g % 256}, ${b % 256},0.5)`;
			},

			inp() {
				// beizhuText:this.beizhuText, 备注内容
				// num 输入金额
				let param = {
					name: this.currentlySelected.name,
					icon: this.currentlySelected.icon,
					type: this.selectIndex,
					createBy: 'admin',
					beizhuText: this.beizhuText,
					num: this.num
				}
				uni.request({
					url: 'api/category/save', // 例如：'https://api.example.com/login'
					method: 'POST',
					data: param,
					success: (res) => {
						if (res.data) {
							// 登录成功，保存用户信息并跳转页面
							uni.setStorageSync('userInfo', res.data); // 假设后端返回一个token
							uni.navigateTo({
								url: '/pages/tabbar/tabbar-5/tabbar-5'
							}); // 跳转到首页或其他页面
						} else {
							uni.showToast({
								title: '登录失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						console.log(param)
						uni.showToast({
							title: '请求失败',
							icon: 'none'
						});
					}
				});


				this.$refs.keyWord.close();
			},
			inp1() {
				// beizhuText 备注内容
				// num 输入金额
				// this.$refs.keyWord.close();
			},
			hide() {},
			goToPage(url) {
				if (!url) return;
				uni.navigateTo({
					url
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.header_top {
		font-size: 24px;
		color: #000;
		padding-top: 23px;
		text-align: center;
		background-color: yellow;
		padding-bottom: 12px;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
	}

	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		z-index: 9999;
	}

	.tabbar-box-wrap {
		position: fixed;
		left: 0;
		bottom: 0;
		overflow: scroll;
		height: 90%;
		border-radius: 16px 16px 0 0;
		z-index: 5;
		background-color: #fff;

		.tabbar-box {
			display: flex;
			padding: 16px;
			flex-wrap: wrap;

			.tabbar-box-item {
				text-align: center;
				margin-bottom: 8px;
				width: 25%;

				.img_warp {
					width: 50px;
					background-color: #ccc;
					height: 50px;
					line-height: 50px;
					color: #fff;
					border-radius: 50%;
					font-size: 36rpx;
					font-weight: 600;
					overflow: hidden;
					margin: auto;
					position: relative;
					text-shadow: 4px 5px 5px black;
				}

				.box-image {
					width: 30px;
					height: 30px;
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					// margin-bottom: 3px;
				}

				.explain {
					text-align: center;
					color: #000;
					font-size: 12px;
					font-weight: bold;
				}

			}
		}
	}

	.tabbarClick {
		display: flex;
		margin-top: 12px;
		color: #ccc;
		justify-content: center;
		// margin: auto;
		position: relative;

		view {
			// flex: 1;
		}
	}
.tabbarClick>.cancel{
	position: absolute;
	right: 16px;
	top: 0;
	color: #000;
	font-size: 14px;
}
	.tabbarClick>view:first-child {
		text-align: right;
		margin-right: 20px;
	}

	.tabbarClick .active {
		color: #000;
	}

	.tabbarClick .active::after {
		display: block;
		content: ' ';
		width: 16px;
		height: 2px;
		background-color: yellow;
		margin: auto;


	}

	.item_active {
		width: 120rpx !important;
		height: 120rpx !important;
		line-height: 120rpx !important;
	}
	.item_active1 {
		font-size: 32rpx !important;
	}

	.keybordWarp {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 1000;
	}

	.money_warp {
		text-align: right;
		font-size: 18px;
		font-weight: bold;
		background: #fff;
		padding: 6px 16px;
		height: 30px;
	}

	.beizhu_warp {
		display: flex;
		justify-content: space-between;
		padding: 4px 16px;
	}

	.beizhu_warp input {
		flex: 1;
	}

	.beizhu_warp text {
		color: #000;
		margin-right: 12px;
	}
</style>